<?php

require_once('Site.class.php');
require_once('Cinema.class.php');

class CinemaManager {

    private $connection;

    const SAVE_NEW_PROJECTION_QUERY = "INSERT INTO cinema (title, story, projections, poster) VALUES (:title, :story, :projections, :poster);";
    const UPDATE_PROJECTION_QUERY = "UPDATE cinema SET title = :title, story = :story, projections = :projections, poster = :poster WHERE id = :id;";
    const DELETE_PROJECTION_QUERY = "DELETE FROM cinema WHERE id = :id;";
    const GET_PROJECTIONS_QUERY = "SELECT * FROM cinema WHERE visible = 1 ORDER BY cinema.order ASC;";
    const GET_ALL_PROJECTIONS_QUERY = "SELECT * FROM cinema;";

    public function __construct() {
        $this->connection = new PDO(
                        "mysql:host=" . Site::DB_HOST . ";dbname=" . Site::DB_NAME,
                        Site::DB_USERNAME,
                        Site::DB_PASSWORD,
                        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
        );
    }

    public function save_cinema_projection(&$p) {
        $statement = $this->connection->prepare(self::SAVE_NEW_PROJECTION_QUERY);
        $statement->execute(array(
            'title' => $p->title,
            'story' => $p->story,
            'projections' => json_encode($p->projections),
            'poster' => $p->poster
                )
        );
        if ($statement->rowCount() <= 0)
            throw new Exception();
        $p->id = $this->connection->lastInsertId();
    }

    public function update_cinema_projections($p) {
        $statement = $this->connection->prepare(self::UPDATE_PROJECTION_QUERY);
        $statement->execute(array(
            'id' => $p->id,
            'title' => $p->title,
            'story' => $p->story,
            'projections' => json_encode($p->projections),
            'poster' => $p->poster
                )
        );
        if ($statement->rowCount() <= 0)
            throw new Exception();
    }

    public function delete_cinema_projection($id) {
        $statement = $this->connection->prepare(self::DELETE_PROJECTION_QUERY);
        $statement->execute(array(
            'id' => $p->id,
                )
        );
        if ($statement->rowCount() <= 0)
            throw new Exception();
    }

    public function get_cinema_projections() {
        $statement = $this->connection->prepare(self::GET_PROJECTIONS_QUERY);
        $statement->execute();
        $result = array();
        $proj = null;

        while ($p = $statement->fetch(PDO::FETCH_ASSOC)) {
            $proj = new Cinema();
            foreach ($p as $key => $value) {
                if ($key == 'projections')
                    $proj->{$key} = json_decode($value, $assoc = true);
                else if ($key == 'visible') {
                    $proj->{$key} = ($value == 1) ? true : false;
                } else {
                    $proj->{$key} = $value;
                }
            }
            array_push($result, $proj);
        }

        return $result;
    }
    
    public function get_all_cinema_projections() {
        $statement = $this->connection->prepare(self::GET_ALL_PROJECTIONS_QUERY);
        $statement->execute();
        $result = array();
        $proj = null;

            while ($p = $statement->fetch(PDO::FETCH_ASSOC)) {
                $proj = new Cinema();
                foreach ($p as $key => $value) {
                    if ($key == 'projections')
                        $proj->{$key} = json_decode($value, $assoc = true);
                    else if ($key == 'visible') {
                        $proj->{$key} = ($value == 1) ? true : false;
                    } else {
                        $proj->{$key} = $value;
                    }
                array_push($result, $proj);
            }

            return $result;
        }
    }

}

?>